package com.party.controller.sys.sysManage;

import com.party.common.Page;
import com.party.model.SysModel;
import com.party.model.SysNav;
import com.party.service.sys.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统顶部菜单
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 12-4-2
 * Time: 下午6:10
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/sys/modelManage")
public class ModelManageController {
    @Resource
    MenuService menusService;
    private int pageSize = 9;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/sysmange/sys_model");
        return modelAndView;
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String name = req.getParameter("name");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }

        SysModel sysModel = new SysModel();
        sysModel.setName(name);
        Page p = this.menusService.getModelList(sysModel, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView add(
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();
        SysModel sysModel = new SysModel();
        List<SysNav> sysNavs = menusService.getNavs();
        modelAndView.addObject("sysNavs", sysNavs);
        modelAndView.setViewName("sys/sysmange/sys_model_add");
        return modelAndView;
    }

    @RequestMapping(value = "/addData", method = RequestMethod.POST)
    public ModelAndView addData(
            @ModelAttribute("sysModel") SysModel sysModel,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();
        int num = menusService.addModel(sysModel);
        String msg = "";
        if (num == 1) {
            msg = "成功!";
        } else {
            msg = "失败!";
        }
        modelAndView.addObject("msg", msg);
        modelAndView.setViewName("redirect:/sys/modelManage/index.do");
        return modelAndView;
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> del(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        int num = 0;
        try {
            num = menusService.delModel(ids);
        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }

        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }

    @RequestMapping(value = "/getModels")
    @ResponseBody
    public Map<String, Object> getModels(
            @RequestParam("navid") String navid,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        SysModel sysModel = new SysModel();
        sysModel.setNavid(Integer.parseInt(navid));
        List<SysModel> sysModels = menusService.getModels(sysModel);

        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("models", sysModels);
        return modelMap;
    }


    @RequestMapping(value = "/update/{modelid}", method = RequestMethod.GET)
    public ModelAndView update(
            @PathVariable int modelid,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();

        SysModel model=menusService.getModel(modelid);
        modelAndView.addObject("list",model);

        List<SysNav> navs = menusService.getNavs();
        modelAndView.addObject("navs",navs);
        SysNav selectedNav = menusService.getNav(modelid);
        modelAndView.addObject("selectedNav",selectedNav);
        modelAndView.setViewName("/sys/sysmange/sys_model_edit");
        return modelAndView;
    }


    /**
     * 编辑数据
     *
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/updateData", method = RequestMethod.POST)
    public ModelAndView updateData(
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();

        String name = request.getParameter("name");
        String sort = request.getParameter("sort");
        String modelid = request.getParameter("modelid");
        String status = request.getParameter("status");
        String code = request.getParameter("code");
        String navid = request.getParameter("navid");

        String msg = "";
        if (navid == null || "".equals(navid) || name == null || sort == null || modelid == null || status == null || "".equals(name) || "".equals("sort") || "".equals(modelid)||"".equals(status)) {
            modelAndView.addObject("msg", "数据非法");
        } else {
            SysModel model = new SysModel();
            model.setModelid(Integer.parseInt(modelid));
            model.setNavid(Integer.parseInt(navid));
            model.setName(name);
            model.setStatus(Integer.parseInt(status));
            model.setSort(Integer.parseInt(sort));
            model.setCode(code);
            int num = menusService.updateModel(model);

            if (num == 1) {
                modelAndView.setViewName("redirect:/sys/modelManage/index.do");
            } else {
                modelAndView.addObject("msg", "数据异常，编辑失败");
                modelAndView.setViewName("/sys/sysmange/sys_nav_edit");
            }
        }
        modelAndView.setViewName("redirect:/sys/modelManage/index.do");
        return modelAndView;
    }
}
